// 加班管理
var overlistColumns = [{
    id: 'createUser',
    title: '申请人',
    type: 'string',
    width: 75,
    columnClass: 'text-center'
}, {
    id: 'proName',
    title: '归属项目',
    type: 'string',
    width: 200,
    columnClass: 'text-center'
}
// , {
//     id: 'totalHour',
//     title: '加班总时长',
//     type: 'string',
//     columnClass: 'text-center'
// }
, {
    id: 'beginTime',
    title: '开始时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var date = new Date(record.beginTime);
        var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy/MM/dd")+" "+
            record.start;
        return content;
    }
}, {
    id: 'endTime',
    title: '结束时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var date = new Date(record.endTime);
        var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy/MM/dd")+" "+
            record.end;
        return content;
    }
}, {
    id: 'comments',
    title: '工作内容',
    type: 'string',
    columnClass: 'text-left'
},{
    id: 'nodeFlag',
    title: '审批状态',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content="";
        if (value=="1"){
            content += '申请中';
        }else if (value=="2"){
            content += '已退回';
        }else if (value=="3"){
            content += '拒绝';
        }else if (value=="4"){
            content += '待审批';
        }else if (value=="5"){
            content += '审批通过';
        }
        return content;
    }
},{
    id: 'createTime',
    title: '申请时间',
    type: 'date',
    width: 110,
    format: 'yyyy-MM-dd',
    otype: 'time_stamp_ms',
    columnClass: 'text-center'
}, {
    id: 'operation',
    title: '操作',
    width: 70,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<a class="btn btn-xs btn-primary" ' +
            'href="javascript:overdetail(\''+record.processInstanceId+'\')"><i class="icon-eye-open"></i>  查看</a>';
        return content;
    }
}];
function findJbWorks($scope, CertusFormAjax) {
    //查询审批人信息
    CertusFormAjax.ajax("findSpinfos",{"code":"%JBLC%"},function(response){
        var data  = response.data;
        var oabtColumns = [{
            id: 'userName',
            title: '审批人',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }
        // ,{
        //     id: 'departmentCode',
        //     title: '所属部门',
        //     type: 'string',
        //     columnClass: 'text-center',
        // }
        ,{
            id: 'company',
            title: '所属公司',
            type: 'string',
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                if (record.company=="HFKJ"){
                    content += '浩方科技';
                }else if (record.company=="HFXX"){
                    content += '浩方信息';
                }else if (record.company=="STSBJ"){
                    content += '赛特斯北京';
                }else if (record.company=="STSGD"){
                    content += '赛特斯广东';
                }else if (record.company=="STSJT"){
                    content += '赛特斯集团';
                }else if (record.company=="STSNJ"){
                    content += '赛特斯南京';
                }else if(record.company=="STSSH"){
                    content += '赛特斯上海';
                }else if(record.company=="SYB-RX"){
                    content += '融合运维事业部';
                }
                return content;
            }
        }, {
            id: 'comments',
            title: '职位',
            type: 'string',
            columnClass: 'text-center',
        }, {
            id: 'approveLevel',
            title: '审批级别',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'minValue',
            title: '天数',
            type: 'string',
            width: 50,
            columnClass: 'text-center',
        }];
        var oabtOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '出差计划列表',
            datas:data,
            columns: oabtColumns,
            gridContainer: 'spContainer',
            toolbarContainer: 'spToolContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        oabtGrid= $.fn.dlshouwen.grid.init(oabtOption);
        oabtGrid.load();
    });


    CertusFormAjax.ajax('findJbworks',{},function (response) {
        var overlistOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '加班申请列表',
            datas: response.data,
            columns: overlistColumns,
            gridContainer: 'overlistContainer',
            toolbarContainer: 'overlistToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        $.fn.dlshouwen.grid.init(overlistOption).load();
    });

    this.overdetail = function (processId) {
        location.href="#/selOver/"+processId;
    }
}

function addOver($scope, CertusFormAjax){
    CertusFormAjax.ajax('initDepts',{},function (response) {
        var data = response.data;
        $scope.depts = data;
        if (data.length==1){
            $("#deptCode").attr("disabled","disabled");
        }
        $scope.deptCode = data[0].departmentCode;
    });

    CertusFormAjax.ajax("findSeProList",{},function(response){
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].proName;
            arr.id = data[i].id;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#proName').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });

    });

    this.calcHours = function (days) {
        var vacationHour = $("#vacationHour").val();
        if (days == "") {
            return;
        }
        var totalHours = Number(days * 8) + Number(vacationHour);
        $("#totalHour").val(totalHours);
    };

    this.calcueDay = function (hours) {
        var vacationDay = $("#vacationDay").val();
        if (vacationDay == "") {
            $("#totalHour").val("");
            return;
        }
        var totalHours = Number(vacationDay * 8) + Number(hours);
        $("#totalHour").val(totalHours);
    };

    //系统计算两日期相差多少个小时
    this.getInervalHour=function(startDate, endDate,beginTime,endTime) {
        if (beginTime==endTime){
            return 0;
        }else{
            var d1 = new Date(startDate);
            var d2 = new Date(endDate);
            return parseInt(d2 - d1) / 1000 / 3600+24;
        }
    };

    $(".submit").on('click', function () {
        // 获取开始日期时间
        var startDay = $("#beginTime").val();
        // 获取结束日期时间
        var endDay = $("#endTime").val();
        if (startDay == "" || startDay == null || startDay == undefined) {
            layer.msg("开始时间不能为空！");
            return;
        }
        if (endDay == "" || endDay == null || endDay == undefined) {
            layer.msg("结束时间不能为空！");
            return;
        }

        CertusFormAjax.ajax('validWorkDay',{"beginTime":startDay,"endTime":endDay},function (result) {
            if (result.data!=""){
                layer.open({
                    title: '提示'
                    ,content: result.data+"为工作日，不能进行加班申请"
                });
                return;
            }else{
                var proId = $("#proId").val();
                var comments = $("#comments").val();
                var vacationDay = $("#vacationDay").val();
                var vacationHour = $("#vacationHour").val();

                var startTime = $("#start option:selected").val();
                var startDT = startDay + " " + startTime;

                var endTime = $("#end option:selected").val();
                var endDT = endDay + " " + endTime;

                if (proId==""){
                    layer.msg("请选择归属项目！");
                    return;
                }

                var diffTHours = getInervalHour(startDT,endDT,startDay,endDay);
                var diffDay = parseInt(diffTHours/24);
                // var diffChours = diffTHours%24;

                if ($("#totalHour").val() == "0") {
                    layer.msg("加班总时长不能为0");
                    return;
                }

                // if (diffDay!=vacationDay){
                //     layer.msg("加班总时长填写不正确，请重新填写！正确日期为："
                //         +diffDay+"天");
                //     return;
                // }

                if (comments.trim() == "" || comments == null || comments == undefined) {
                    layer.msg("工作内容不能为空！");
                    return;
                }

                // 转换开始标准时间
                var start = new Date(startDT);
                // 转换结束标准时间
                var end = new Date(endDT);

                if (Date.parse(end) == Date.parse(start)) {
                    layer.msg("结束时间不能等于开始时间！");
                    return;
                }
                if (Date.parse(end) < Date.parse(start)) {
                    layer.msg("结束时间不能小于开始时间！");
                    return;
                }

                // 截取时间保留分钟
                var starMinutes = Number(startTime.substring(3, 5));
                var endMinutes = Number(endTime.substring(3, 5));
                if (starMinutes != endMinutes) {
                    layer.msg("请修改加班起始时间，保证期间总时间为1小时的倍数。目前公司规定加班登记最小单位为1个小时");
                    return;
                }
                var arr = $("form").serializeArray();
                var data = {};
                for (var i = 0; i < arr.length; i++) {
                    data[arr[i].name] = arr[i].value;
                };
                data.totalHour = $("#totalHour").val();
                data.deptCode = $("#deptCode").val();
                data.proName = $("#proName").val();
                // 开启
                var index = layer.load(1);
                CertusFormAjax.ajax('addOver', {
                    "param": JSON.stringify(data)
                }, function () {
                    //关闭
                    layer.close(index);
                    layer.msg("申请成功");
                    window.history.back()
                });
            }
        });
    });
};
